package com.jinrustar.httpfileserver.core;


import com.jinrustar.httpfileserver.core.util.WebUtil;
import com.jinrustar.httpfileserver.springboot.Application;
import com.jinrustar.httpfileserver.util.CommonUtil;
import com.jinrustar.httpfileserver.util.FileUtil;
import com.jinrustar.httpfileserver.core.http.HttpFileServer;
import com.jinrustar.httpfileserver.core.util.qrcode.QRCodeUtil;
import com.jinrustar.httpfileserver.core.ws.WebSocketServer;
import com.jinrustar.httpfileserver.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import static com.jinrustar.httpfileserver.core.http.HttpFileServer.ipAndPort;
import static com.jinrustar.httpfileserver.springboot.Application.threadPool;

public class HttpFileServerApp {
    private final static int PORT = 5556;
    public final static String DIR = System.getProperty("user.home") + "/Upload/";

    private static Logger logger = LoggerFactory.getLogger(HttpFileServerApp.class);

    public static final int WS_SERVER_PORT = 7379;

    public static final String QR_IMG_PATH = DIR + "wankeyun_qr.jpg";


    public static void main(String... args) {
        startHttpServerAndWebSocketServer(Application.startSuccessCallback);
    }

    public static void startHttpServerAndWebSocketServer(Application.StartSuccessCallback startSuccessCallback) {
        /*http server*/
        HttpFileServer httpFileServer = new HttpFileServer();

        threadPool.execute(() -> {
            try {
                httpFileServer.run(PORT, DIR, (ip, serviceTypeName) -> {
                    startSuccessCallback.onSuccess(ip, serviceTypeName);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        });


        WebSocketServer webSocketServer = new WebSocketServer();

        /*websocket server*/
        threadPool.execute(() -> {
            try {
                webSocketServer.run(HttpFileServerApp.WS_SERVER_PORT, (ip, serviceTypeName) -> {
                    startSuccessCallback.onSuccess(ip, serviceTypeName);
                });
            } catch (UnknownHostException | InterruptedException e) {
                e.printStackTrace();
            }
        });


    }

    public static void createQrCodeImgForFileBrowserUrl(String frontendFileBrowserUrl, String logoImgPath, String destQrImgPath) throws Exception {
        String base64edFileUrl = CommonUtil.encodeToStringByBase64(frontendFileBrowserUrl.getBytes());
        base64edFileUrl = base64edFileUrl.trim().replaceAll("\\n", "");
        String qrcodeUrl = "http://mm1.jinrustar.com/to-xunlei-wanke?_U=" + base64edFileUrl + "_t";
        logger.info("##f:{}", base64edFileUrl);
        File logoFile = new File(logoImgPath);
        File destQrImgFile = new File(destQrImgPath);
        QRCodeUtil.encode(qrcodeUrl, logoFile, true, destQrImgFile);

        logger.info("###qr image path:{}", destQrImgFile.getAbsolutePath());
    }

    public static boolean exists(String path) {
        return new File(DIR + path).exists();
    }

    public static boolean isFile(String path) {
        return new File(DIR + path).isFile();
    }

    public static List<FileUtil.FileInfo> listFiles(String path) {
        File file = new File(DIR + path);
        if (file.isFile()) {
            return null;
        }
        File[] files = file.listFiles(pathname -> !pathname.isHidden());
        List<FileUtil.FileInfo> fileInfos = new ArrayList<>();
        for (File f : files) {
            String name = f.getName();
            boolean isDir = f.isDirectory();
            long length = f.length();
            long d = f.lastModified();
            fileInfos.add(new FileUtil.FileInfo(name, isDir, length, d));
        }
        return fileInfos;
    }
}
